<template>
    <div id="LoginCountChart" :style="{ width: props.width, height: props.height }"></div>
</template>
<script setup lang="ts">
import { GetLoginData } from '@/controllers/adminController';
import { onMounted } from 'vue';
import * as echarts from 'echarts';
const props = defineProps<{ width: string, height: string }>();

onMounted(() => {
    try {
        drawChart();
    } catch (error) {
        console.error(error);
    }
})
async function drawChart() {
    const loginData = await GetLoginData()
    const c = document.getElementById('LoginCountChart')
    if (!c) {
        console.error('元素不存在！')
        return;
    }
    const chart = echarts.init(c);
    const option: echarts.EChartsOption = {
        title: {
            text: '近一周用户登录统计'
        },
        tooltip: {
            trigger: 'axis'
        },
        legend: {
            data: ['教师', '学生', '管理员']
        },
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        toolbox: {
            feature: {
                saveAsImage: {}
            }
        },
        xAxis: {
            type: 'category',
            boundaryGap: false,
            data: loginData.dates
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name: '教师',
                type: 'line',
                stack: 'Total',
                data: loginData.teacherLoginCounts
            },
            {
                name: '学生',
                type: 'line',
                stack: 'Total',
                data: loginData.studentLoginCounts
            },
            {
                name: '管理员',
                type: 'line',
                stack: 'Total',
                data: loginData.adminLoginCounts
            }
        ]
    };
    chart.setOption(option)
}
</script>